package creating_user_interface;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

public class CheckBoxDemo extends ButtonDemo {
	private JCheckBox jchkCentered = new JCheckBox("Centered");
	private JCheckBox jchkBold = new JCheckBox("Bold");
	private JCheckBox jchkItalic = new JCheckBox("Italic");
	
	public static void main(String[] args) {
		CheckBoxDemo frame = new CheckBoxDemo();
		frame.setTitle("CheckBoxDemo");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setSize(500, 200);
		frame.setVisible(true);
	}
	public CheckBoxDemo() {
		// Set mnemonic keys
		jchkCentered.setMnemonic('C');
		jchkBold.setMnemonic('B');
		jchkItalic.setMnemonic('I');
		
		// Create a new panel to hold check boxes
		JPanel jpCheckBoxes = new JPanel();
		jpCheckBoxes.setLayout(new GridLayout(3, 1));
		jpCheckBoxes.add(jchkCentered);
		jpCheckBoxes.add(jchkBold);
		jpCheckBoxes.add(jchkItalic);
		add(jpCheckBoxes, BorderLayout.EAST);
		
		//Register listeners with the check boxes
		jchkCentered.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				messagePanel.setCentered(jchkCentered.isSelected());
			}
		});
		jchkBold.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				setNewFont();	
			}
		});
	}
	private void setNewFont() {
		//Determine a font style
		int fontStyle = Font.PLAIN;
		fontStyle += (jchkBold.isSelected()? Font.BOLD : Font.PLAIN);
		fontStyle += (jchkItalic.isSelected() ? Font.ITALIC : Font.PLAIN);
		
		//Set font for the message
		Font font = messagePanel.getFont();
		messagePanel.setFont((new Font(font.getName(), fontStyle, font.getSize())));
	}
}
